﻿
/**
* Masque Layout Library 0.0.2.5
* Copyright(c) 2008, 青岛领创网络科技有限公司
* @author Lookingon Team
*/
/// <reference path="LM_Base.js" />
if (Masque && !Masque.L) {
    Masque.L = {
        MoveDirection: {
            Up: 0, //向上
            Right: 1, //向右
            Down: 2, //向下
            Left: 3, //向左
            Parse: function(n) {
            },
            isDefined: function(n) {
            }
        },
        PositionDirection: {
            Up: 0, //向上
            Right: 1, //向右
            Down: 2, //向下
            Left: 3, //向左
            RightUp: 4, //向上
            RightDown: 5, //向右
            LeftUp: 6, //向下
            LeftDown: 7, //向左
            DownRight: 8, //底部左侧对齐
            Parse: function(n) {
            },
            isDefined: function(n) {
            }
        },
        ShowAgainst: function(_obj, _layer, _Position, _offsetTop, _offsetLeft) {
            if (_obj == null || _layer == null) {
                return;
            }
            if (_layer.style.position != "absolute")
                _layer.style.position = "absolute";
            if (_layer.style.display != "block")
                _layer.style.display = "block";
            var top = 0;
            var left = 0;

            var p = GetPosition(_obj);
            top = p.Top;
            left = p.Left;
            switch (_Position) {
                case this.PositionDirection.Up:
                    //top -= _obj.clientHeight;
                    break;
                case this.PositionDirection.Down:
                    top += _obj.clientHeight;
                    break;
                case this.PositionDirection.Left:
                    //left -= (Masque.W.GetVisibleWidth(_obj) );
                    break;
                case this.PositionDirection.RightUp:
                    left += _obj.clientWidth;
                    top -= _obj.clientHeight;
                    break;
                case this.PositionDirection.RightDown:
                    left -= _obj.clientWidth;
                    top += _obj.clientHeight;
                    break;
                case this.PositionDirection.LeftUp:
                    left -= _obj.clientWidth;
                    top -= _obj.clientHeight;
                    break;
                case this.PositionDirection.LeftDown:
                    left -= _obj.clientWidth;
                    top += _obj.clientHeight;
                    break;
                case this.PositionDirection.DownRight:
                    left = left + Masque.W.GetVisibleWidth(_obj) - _layer.clientWidth;
                    top += Masque.W.GetVisibleHeight(_obj);
                default:
                    left += Masque.W.GetVisibleWidth(_obj);
                    break;
            }
            if (Masque.V.isNumber(_offsetTop))
                top += _offsetTop;
            if (Masque.V.isNumber(_offsetLeft))
                left += _offsetLeft;
            if (top < 0)
                top = 0;
            if (left < 0)
                left = 0;

            if (_layer.style.left != left + "px")
                _layer.style.left = left + "px"
            if (_layer.style.top != top + "px")
                _layer.style.top = top + "px"

        },
        ShowPopup: function(_obj, _layer, _Position, _offsetTop, _offsetLeft) {
            var top = 0;
            var left = 0;
            if (_obj == null || _layer == null) {
                return;
            }
            if (_layer.style.display != "block")
                _layer.style.display = "block";

            var p = GetPosition(_obj, _layer.offsetParent);
            top = p.Top;
            left = p.Left;
            switch (_Position) {
                case this.PositionDirection.Up:
                    //top -= _obj.clientHeight;
                    break;
                case this.PositionDirection.Down:
                    top += _obj.clientHeight;
                    break;
                case this.PositionDirection.Left:
                    //left -= Masque.W.GetVisibleWidth(_obj);
                    break;
                case this.PositionDirection.RightUp:
                    left += _obj.clientWidth;
                    top -= _obj.clientHeight;
                    break;
                case this.PositionDirection.RightDown:
                    left -= _obj.clientWidth;
                    top += _obj.clientHeight;
                    break;
                case this.PositionDirection.LeftUp:
                    left -= _obj.clientWidth;
                    top -= _obj.clientHeight;
                    break;
                case this.PositionDirection.LeftDown:
                    left -= _obj.clientWidth;
                    top += _obj.clientHeight;
                    break;
                case this.PositionDirection.DownRight:
                    left = left + Masque.W.GetVisibleWidth(_obj) - _layer.clientWidth;
                    top += Masque.W.GetVisibleHeight(_obj);
                    break;
                default:
                    left += _obj.clientWidth;
                    break;
            }
            if (top < 0)
                top = 0;
            if (left < 0)
                left = 0;
            if (Masque.V.isNumber(_offsetTop))
                top += _offsetTop;
            if (Masque.V.isNumber(_offsetLeft))
                left += _offsetLeft;

            if (_layer.style.position != "absolute")
                _layer.style.position = "absolute";
            if (_layer.style.left != left + "px")
                _layer.style.left = left + "px"
            if (_layer.style.top != top + "px")
                _layer.style.top = top + "px"
            var me = this;
            var a = function() {
                if (window.event.srcElement.getAttribute("Pupup") == 'true') {
                    if (window.event.button == 2) {
                        removeEvent(document, "click", a)
                        _layer.style.display = 'none';
                    }
                }
                else {
                    _layer.style.display = 'none';
                    removeEvent(document, "click", a)
                }

            };
            window.setTimeout(function() { addEvent(document, "click", a) }, 50);

        }

    };
}



//使用说明，必须保证控件加载完成后再调用类进行操作。
Masque.L.Scrollor = function() {
    /*参数说明
    * (_ScrollBox)
    * (_ScrollBox,_Speed,_Step)
    * (_ScrollBox,_AutoStop)
    * (_ScrollBox,_AutoStop,_IsRoll)
    * (_ScrollBox,_AutoStop,_IsRoll,_Direction)
    * (_ScrollBox,_AutoStop,_IsRoll,_Direction,_Speed,_Step)
    * _ScrollBox：受控的HTML容器。object类型，为string类型时则认为是对像的ID。
    * _Speed：滚动间隔， 数值越大，速度越慢。int类型
    * _Step：滚动长度，数值越大，速度越快。int类型
    * _AutoStop：在鼠标移到到容器时，是否自动停止滚动,可以调用SetAutoStop进行控制。bool类型
    * _IsRoll：是否循环滚动，可以调用SetRoll控制。boll类型
    * _Direction：滚动方向， 枚举自Masque.L.MoveDirection。也可以为0-3的整数
    */
    this.Speed = 10; //滚动间隔， 数值越大，速度越慢。int类型
    this.Step = 1; //滚动长度，数值越大，速度越快。int类型
    this.Direction = Masque.L.MoveDirection.Up; //滚动方向， 枚举自Masque.L.MoveDirection。也可以为0-3的整数。
    this.isRoll = false; //是否循环滚动（仿marquee），可以调用SetRoll控制。boll类型
    this.ScrollBox = null; //受控的HTML容器。object类型，为string类型时则认为是对像的ID。
    this.__runTimer = 0; //定时器指针
    this.autoStop = true; //在鼠标移到到容器时，是否自动停止滚动,可以调用SetAutoStop进行控制。bool类型
    if (typeof Masque.L.Scrollor._initialized == "undefined") {
        Masque.L.Scrollor.prototype.MoveUp = function() {
            var me = this;
            me.Direction = Masque.L.MoveDirection.Up;
            me.__runTimer = window.setInterval(function() {
                me.__doMove();
            }, me.Speed);

        };
        Masque.L.Scrollor.prototype.MoveRight = function() {
            var me = this;
            me.Direction = Masque.L.MoveDirection.Right;
            me.__runTimer = window.setInterval(function() {
                me.__doMove();
            }, me.Speed);
        };
        Masque.L.Scrollor.prototype.MoveDown = function() {
            var me = this;
            me.Direction = Masque.L.MoveDirection.Down;
            me.__runTimer = window.setInterval(function() {
                me.__doMove();
            }, me.Speed);
        };
        Masque.L.Scrollor.prototype.MoveLeft = function() {
            var me = this;
            me.Direction = Masque.L.MoveDirection.Left;
            me.__runTimer = window.setInterval(function() {
                me.__doMove();
            }, me.Speed);
        };
        Masque.L.Scrollor.prototype.__doMove = function() {
            var me = this;

            var cb = Masque.D.getChildNodes(me.ScrollBox, 0);
            if (cb != null) {

                switch (me.Direction) {
                    case Masque.L.MoveDirection.Right:
                        if (me.isRoll) {
                            if (me.ScrollBox.scrollLeft <= cb.offsetWidth - me.ScrollBox.clientWidth) {

                                me.ScrollBox.scrollLeft = cb.offsetWidth * 2 - me.ScrollBox.clientWidth;
                            }
                            else {
                                me.ScrollBox.scrollLeft += me.Step;
                            }
                        }
                        else {
                            if (Masque.isFirefox) {
                                var fl = me.ScrollBox.scrollLeft;
                                //var gap = me.ScrollBox.clientWidth - cb.offsetWidth;
                                me.ScrollBox.scrollLeft += me.Step;
                                if (me.ScrollBox.scrollLeft == fl) {
                                    me.Stop();
                                }

                            }
                            else {
                                if (me.ScrollBox.scrollLeft < me.ScrollBox.scrollWidth - me.ScrollBox.clientWidth) {
                                    me.ScrollBox.scrollLeft += me.Step;
                                }
                                else {
                                    me.Stop();
                                }
                            }
                        }
                        break;
                    case Masque.L.MoveDirection.Down:
                        if (me.isRoll) {
                            if (me.ScrollBox.scrollTop <= cb.offsetHeight - me.ScrollBox.clientHeight) {
                                me.ScrollBox.scrollTop = cb.offsetHeight * 2 - me.ScrollBox.clientHeight;
                            }
                            else {
                                me.ScrollBox.scrollTop -= me.Step;
                            }
                        }
                        else {
                            if (me.ScrollBox.scrollTop > 0) {
                                me.ScrollBox.scrollTop -= me.Step;
                            }
                            else {
                                me.Stop();
                            }
                        }
                        break;
                    case Masque.L.MoveDirection.Left:


                        if (this.isRoll) {
                            if (me.ScrollBox.scrollLeft >= cb.offsetWidth + this.ScrollBox.clientWidth) {

                                me.ScrollBox.scrollLeft = 0;
                            }
                            else {
                                me.ScrollBox.scrollLeft -= me.Step;
                            }
                        }
                        else {
                            if (me.ScrollBox.scrollLeft > 0) {
                                me.ScrollBox.scrollLeft -= me.Step;
                            }
                            else {
                                me.Stop();
                            }
                        }
                        break;
                    default:
                        if (this.isRoll) {
                            if (cb.offsetHeight - this.ScrollBox.scrollTop <= 0) {

                                me.ScrollBox.scrollTop = 0;
                            }
                            else {
                                me.ScrollBox.scrollTop += me.Step;
                            }
                        }
                        else {

                            if (me.ScrollBox.scrollHeight - me.ScrollBox.scrollTop > me.ScrollBox.clientHeight) {
                                me.ScrollBox.scrollTop += me.Step;

                            }
                            else {
                                me.Stop();
                            }
                        }
                        break;
                }
            }
        };

        Masque.L.Scrollor.prototype.__startMove = function() {

            switch (me.Direction) {
                case Masque.L.MoveDirection.Right:
                    this.MoveRight();
                    break;
                case Masque.L.MoveDirection.Down:
                    this.MoveDown();
                    break;
                case Masque.L.MoveDirection.Left:
                    this.MoveLeft();
                    break;
                default:
                    this.MoveUp();
                    break;
            }
        };
        Masque.L.Scrollor.prototype.SetRoll = function(roll) {
            if (roll == true) {
                if (!this.isRoll) {
                    this.isRoll = true;
                    var copyObject = Masque.D.getChildNodes(this.ScrollBox, 0);

                    if (copyObject != null) {
                        copyObject = copyObject.cloneNode(this.ScrollBox);
                        this.ScrollBox.appendChild(copyObject);
                    }
                    //初始化位置 未完成。
                    this.__startMove();
                }
            }
            else {
                if (this.isRoll) {
                    this.isRoll = false;
                    var copyObject = Masque.D.getChildNodes(this.ScrollBox, 1);

                    if (copyObject != null) {
                        document.removeChild(copyObject);
                    }
                }

            }
        };
        Masque.L.Scrollor.prototype.SetAutoStop = function(autoStop) {
            var me = this;

            if (autoStop == true) {
                if (!me.autoStop) {
                    addEvent(me.ScrollBox, "mouseover", me.Stop);
                    addEvent(me.ScrollBox, "mouseout", me.__doMove);
                }
            }
            else {
                if (me.autoStop == true) {
                    removeEvent(me.ScrollBox, "mouseover", me.Stop);
                    removeEvent(me.ScrollBox, "mouseout", me.__doMove);
                }

            }
        };
        Masque.L.Scrollor.prototype.Stop = function() {
            if (this.__runTimer > 0) {
                window.clearInterval(this.__runTimer);
                this.__runTimer = 0;
            }
        };
    }
    Masque.L.Scrollor.prototype._initialized = true;

    //初始化参数。
    if (arguments.length > 0) {
        var me = this;
        me.ScrollBox = $(arguments[0]);
        //        if (me.ScrollBox.style.overflow != "hidden") 
        //            me.ScrollBox.style.overflow = "hidden";
        if (me.ScrollBox.style.width == "")
            me.ScrollBox.style.width = "100%";
        if (me.ScrollBox.style.height == "")
            me.ScrollBox.style.height = me.ScrollBox.parentNode.clientHeight;
    }
    switch (arguments.length) {
        case 2:
            this.SetAutoStop(arguments[1]);
            break;
        case 3:
            if (Masque.V.isNumber(arguments[1]) && Masque.V.isNumber(arguments[2])) {
                this.Speed = arguments[1];
                this.Step = arguments[2];
            }
            else {
                this.SetAutoStop(arguments[1]);
                this.SetRoll(arguments[2]);
            }
            break;

        case 4:
            var d = arguments[3];
            if (d >= 0 && d < 4) {
                this.Direction = d;
            }
            this.SetAutoStop(arguments[1]);
            this.SetRoll(arguments[2]);
            break;
        case 6:
            var d = arguments[3];
            if (d >= 0 && d < 4) {
                this.Direction = d;
            }
            if (Masque.V.isNumber(arguments[4])) {
                this.Speed = arguments[4];
            }
            if (Masque.V.isNumber(arguments[5])) {
                this.Step = arguments[5];
            }

            this.SetAutoStop(arguments[1]);
            this.SetRoll(arguments[2]);

            break;
    }


}





